package com.gopay.bis.manager.bank.ebank;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.CmbcConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.cmbc.ebank.CmbcB2bMessage;
import com.gopay.common.domain.bank.cmbc.ebank.CmbcMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankManagerRemoteService;
import com.gopay.remote.bank.businesses.BankService;

public class CmbcB2bManager implements BankManagerRemoteService {
	
	@Resource(name="cmbcB2bBankServiceClient")
	private BankService cmbcB2bBankServiceClient;
	
	@Resource(name="cmbcB2bBankServiceClientHna")
	private BankService cmbcB2bBankServiceClientHna;

	 @Resource(name = "cmbcConfig")
	 private Map<String, String> cmbcConfig;

	@Override
	public Object sign(BaseMessage msg) throws GopayException {
		String signMessage = "";
		CmbcMessage  cmbcMessage = buildCmbcMessage((CmbcMessage) msg);
		BankLogUtils.printB2BManagerSignSendLog(cmbcMessage);
		if(cmbcMessage.isHna()){
			signMessage = (String) cmbcB2bBankServiceClientHna.sign(cmbcMessage);
		}else{
			signMessage = (String) cmbcB2bBankServiceClient.sign(cmbcMessage);
		}
		cmbcMessage.setSignMessage(signMessage);
		if (!StringUtils.isEmpty(signMessage)) {
			String[] tempStr = signMessage.split("\\|");
			cmbcMessage.setOrderStatus(tempStr[0]);
			cmbcMessage.setResMessage(tempStr[1]);
			cmbcMessage.setResCode(tempStr[2]);
		}
		BankLogUtils.printB2BManagerSignReturnLog(cmbcMessage);
		return cmbcMessage;
	}

	private CmbcMessage buildCmbcMessage(CmbcMessage message) {
		message.setTrnxCode(CmbcConstants.TRANSFER_BUSINESS_CODE); // P006支付交易
		message.setResultNotifyURL(message.getPayRuturnUrl());
		message.setSignFlag("1");
		message.setDigest("b2bPay");
		message.setSellerBank((String) cmbcConfig.get("CMBC.B2B.SELLERBANK"));
		message.setSellerBankAddress((String) cmbcConfig.get("CMBC.B2B.SELLERBANKADDRESS"));
		message.setSellerCorporationAcctNo((String) cmbcConfig.get("CMBC.B2B.SELLERCORPORATIONACCTNO"));
		message.setSellerCorporationAcctName((String) cmbcConfig.get("CMBC.B2B.SELLERCORPORATIONACCTNAME"));
		message.setSellerCorporationName((String) cmbcConfig.get("CMBC.B2B.SELLERCORPORATIONNAME"));
		message.setPayType("0");
		message.setOperName("jy");
		message.setCustomerID(message.getCustomerID());
		return message;
	}

	@Override
	public Object verify(BaseMessage msg) throws GopayException {
		CmbcB2bMessage cmbcMessage = (CmbcB2bMessage) msg;
		BankLogUtils.printB2BManagerVerifySendLog(msg);
		boolean flag = false;
		String isVerify;
		if(cmbcMessage.isHna()){
			isVerify = (String) cmbcB2bBankServiceClientHna.verify(cmbcMessage);
		}else{
			isVerify = (String) cmbcB2bBankServiceClient.verify(cmbcMessage);
		}
		HashMap map = new HashMap();
		if(isVerify!=null && !"".equals(isVerify)){
			cmbcMessage.setPlainMessage(isVerify);
			String[] str =getMessage(isVerify);
			cmbcMessage.setOrderId(str[0]);
			cmbcMessage.setBankOrderId(str[0]);
			cmbcMessage.setAmount(new BigDecimal(str[1]));
			cmbcMessage.setOrderStatus(str[2]);
			flag = true;
			map.put("flag",flag);
			map.put("cmbcMessage",cmbcMessage);
		}
		BankLogUtils.printB2BManagerVerifyReturnLog(msg, isVerify);
		return map;
	}

	@Override
	public Object query(Object obj) throws GopayException {
		CmbcMessage cmbcMessage = (CmbcMessage)obj;
		CmbcMessage resMessage = null;
		BankLogUtils.printB2BManagerQuerySendLog(cmbcMessage, "");
		if(cmbcMessage.isHna()){
			resMessage = (CmbcMessage) this.cmbcB2bBankServiceClientHna.query(cmbcMessage);
		}else{
			resMessage = (CmbcMessage) this.cmbcB2bBankServiceClient.query(cmbcMessage);
		}
		BankLogUtils.printB2BManagerQueryReturnLog(cmbcMessage);
		return resMessage;
	}
	
	public String [] getMessage(String msg){
		String [] str =msg.split("\\|");
		return str;
	}
	
	public BankService getCmbcB2bBankServiceClient() {
		return cmbcB2bBankServiceClient;
	}

	@Override
	public Object dayend(BaseMessage base) throws GopayException, Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object refund(BaseMessage base) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

}
